<?php


trait View {
	
	
	
	public static function getTitre($type){
		if(isset(self::$titres[$type])){
			return self::$titres[$type];
		}
		return '';
	}
	
	
	public static function getChamps($type=null){
	    if($type===null){
		return self::$champs;
	    }
	    return self::$champs[$type];
	}
        
	
	public static function getFormulaire(){
		$class=get_called_class();
		
		$isEdit=false;
		$entity=new $class();
		
		//on vérifie si on est pas en mode édition
		if(isset($_GET['id'])){
			$selected=$class::load($_GET['id']);
			if($selected != null){
				$isEdit = true;
				$entity = $selected;
			}
		}
		
		$formulaire='<h3>'.$class::getTitre($isEdit?'edit':'new').'</h3>'.PHP_EOL;
		
		$formulaire.='<form method="post" enctype="multipart/form-data">'.PHP_EOL;
		foreach($class::getChamps() as $nom => $params){
			if($params['type']=='hidden'){
				$id=$isEdit?$entity->$nom:'';
				$formulaire.="<input name='$nom' type='hidden' value='$id' />".PHP_EOL;
			}
			else{
				$value=$entity->$nom;
				$type=$params['type'];
				$metas=$entity::getPropertyMetas($nom);
				$ajoutLabel=true;
				$balise='';
				
				if($type=='text' || $type=='date' || $type=='password'){
					$balise.="<input name='$nom' type='$type' value='$value' />";
				}
				elseif($type=='number'){
					if(!$value){
						$value=isset($params['default'])?$params['default']:'';
					}
					$min=isset($params['min'])?' min="'.$params['min'].'"':'';
					$max=isset($params['max'])?' max="'.$params['max'].'"':'';
					$help=isset($params['help'])?$params['help']:'';
					$type=$params['type'];
					$balise.="<input name='$nom' type='number' $min $max value='$value'/> $help";
				}
				elseif($type=='time'){
					$value=Date::formaterTime($value);
					$balise.="<input name='$nom' type='time' value='$value' />";
				}
				elseif($type=='textarea'){
					$balise.="<textarea name='$nom'>$value</textarea>";
				}
				elseif($type=='boolean'){
					$chechedY=$value=='Y'?' checked':'';
					$chechedN=$value!='Y'?' checked':'';
					$balise.="<label><input name='$nom' type='radio' value='Y'$chechedY/> Oui</label>";
					$balise.="<label><input name='$nom' type='radio' value='N'$chechedN/> Non</label>";
					$ajoutLabel=false;
				}
				elseif($type=='float'){
					$step=$params['precision'];
					$balise.="<input name='$nom' type='number' value='$value' step='$step' />";
				}
				elseif($type=='image'){
					//$maxSize=ini_get('upload_max_filesize');
					$balise.='<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />';
					$balise.="<input name='$nom' type='file'/>";
					if($value){
						$balise.="<br/><img height='100px' src='$value'/>";
					}
				}
				else{
					//il s'agit d'un Foreign Key de classe $type
					
					$balise.="<select name='$nom'>".PHP_EOL;
					$elements=$type::loadAll();
					//$balise.='  <option value="null"></option>'.PHP_EOL; //si nullable
					//else if(count($elements)==0){
					if(count($elements)==0){
						Message::warn('La liste de "'.$params['label'].'" est vide.
							Vous devez en créer avant de pouvoir '.mb_strtolower($class::getTitre('new'),'UTF-8'));
					}
					foreach($elements as $element){
						$pk=$type::getPK();
						$id=$element->$pk;
						$selected=($value==$id)?' selected':'';
						$balise.="  <option value='$id'$selected>$element</option>".PHP_EOL;
					}
					$balise.='</select>';
				}
				$comment=' ';
				if($metas['comment']!=''){
					$comment.='<span class="comment">'.$metas['comment'].'</span>';
				}
				if($ajoutLabel) $formulaire.='<label>';
				$formulaire.=Texte::toHtml($params['label']).' : '.$balise.$comment;
				if($ajoutLabel) $formulaire.='</label>';
				$formulaire.='<br/>'.PHP_EOL;
			}

		}
		$formulaire.='<a href="'.Navig::getPageName().'#'.SvcCRUDConnected::ANCHOR_LIST.'">Annuler</a>'.PHP_EOL;
		$formulaire.='<button type="submit" name="action" value="save">Enregistrer</button>'.PHP_EOL;
		$formulaire.='</form>'.PHP_EOL;
		
		return $formulaire;
	}
	
	
	
	
	public static function getListTable(){
		$class=get_called_class();
		$pk=$class::getPK();
		
		$elements=Pagination::getList($class);
		
		$table='<h3>'.Texte::toHtml($class::getTitre('list')).'</h3>'.PHP_EOL;
		
		if(count($elements)==0){
			return $table.Texte::toHtml("Il n'y a aucun élément à afficher");
		}
        
        $table.=Pagination::getNavigation($class);
		
		$table.='<table class="liste" border="1">'.PHP_EOL;
		$table.=' <thead>'.PHP_EOL;
		$table.='  <tr>'.PHP_EOL;
		$colonnes=array();
		foreach($class::getChamps() as $nom => $params){
			if(isset($params['show']) && $params['show']){
				$colonnes[]=$nom;
                $table.=Pagination::getSortableHeader($nom,$params['label']);
				//$table.='   <th>'.$params['label'].'</th>'.PHP_EOL;
			}
		}
		$table.='   <th>Action</th>'.PHP_EOL;
		$table.='  </tr>'.PHP_EOL;
		$table.=' </thead>'.PHP_EOL;
		$table.=' <tbody>'.PHP_EOL;
		
		foreach($elements as $element){
			$table.='  <tr>'.PHP_EOL;
			foreach($colonnes as $colonne){
				$params=$class::getChamps()[$colonne];
				$type=$params['type'];
				$value=$element->$colonne;
				if($type=='time') $value=Date::formaterTime($value);
				if($type=='date') $value=Date::formaterDate($value);
				if($type=='image'){
					$value="<img height='100px' src='$value'/>";
				}
				if(isset($params['entity'])){
					//il s'agit d'un Foreign Key de classe $type
					$entityCall=$params['entity'];
					$value=$element->$entityCall;
				}
				$table.="   <td>$value</td>".PHP_EOL;
			}
			$editLink=Navig::getPageName().'?id='.$element->$pk.'#'.SvcCRUDConnected::ANCHOR_EDIT;
			$table.='   <td>'.PHP_EOL;
			if(!isset($class::$blockModif)){
				$table.='	<a href="'.$editLink.'">Modifier</a>'.PHP_EOL;
			}
			if(!isset($class::$blockDelete)){
				$table.='	<a href="#" onclick="supprimer(this)">Supprimer</a>'.PHP_EOL;
			}
			$table.='   </td>'.PHP_EOL;
			$table.='  </tr>'.PHP_EOL;
		}
		$table.=' </tbody>'.PHP_EOL;
		$table.='</table>'.PHP_EOL;
		
		return $table;
	}
	
}



?>